import re
from django import forms
class LoginForm(forms.Form):

    domain = forms.CharField(label='域名',max_length=50)
    username = forms.CharField(label='账号',max_length=50)
    password = forms.CharField(label='密码',max_length=50,widget=forms.PasswordInput)

    def clean_domain(self):
      domain = self.cleaned_data.get('domain')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(domain) is not None:
            raise forms.ValidationError('域名只能包含数字或者字母，请重新输入！')
      else:
        if len(domain) < 3 or len(domain) > 20:
            raise forms.ValidationError("域名应在3-20位之间，请重新输入！")
      return domain
    def clean_username(self):
        username = self.cleaned_data.get('username')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(username) is not None:
            raise forms.ValidationError('账号只能包含数字或者字母，请重新输入！')
        else:
            if len(username) < 3 or len(username) > 20:
                raise forms.ValidationError("域名应在3-20位之间，请重新输入！")
        return username
    def clean_password(self):
        password = self.cleaned_data.get('password')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(password) is not None:
            raise forms.ValidationError('密码只能包含数字和字母，请重新输入！')
        else:
            if len(password) < 3 or len(password) > 20:
                raise forms.ValidationError('密码应在3-20位之间，请重新输入！')
        return password

#创建项目表单
class ProjectcreateForm(forms.Form):

    projectname = forms.CharField(label='名称',max_length=50)
    description = forms.CharField(label='描述',max_length=50)

    def clean_name(self):
      projectname = self.cleaned_data.get('projectname')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(projectname) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(projectname) < 3 or len(projectname) > 20:
            raise forms.ValidationError("名称应在3-20位之间，请重新输入！")
      return projectname
    def clean_description(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description

#修改项目表单
class ProjectupdateForm(forms.Form):

    projectname = forms.CharField(label='名称',max_length=50)
    description = forms.CharField(label='描述',max_length=50)

    def clean_name(self):
        projectname = self.cleaned_data.get('projectname')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(projectname) is not None:
            raise forms.ValidationError('项目名称只能包含数字或者字母，请重新输入！')
        else:
            if len(projectname) < 3 or len(projectname) > 20:
                raise forms.ValidationError("项目名称应在3-20位之间，请重新输入！")
        return projectname

    def description_name(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search( description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description

class UsercreateForm(forms.Form):

    user = forms.CharField(label='用户名',max_length=50)
    description = forms.CharField(label='描述',max_length=50)
    password = forms.CharField(label='密码', max_length=50, widget=forms.PasswordInput)
    email = forms.EmailField(label='邮箱',max_length=50)

    def clean_name(self):
      user = self.cleaned_data.get('user')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(user) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(user) < 3 or len(user) > 20:
            raise forms.ValidationError("名称应在3-20位之间，请重新输入！")
      return user
    def clean_description(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description

    def clean_password(self):
        password = self.cleaned_data.get('password')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(password) is not None:
            raise forms.ValidationError('密码只能包含数字和字母，请重新输入！')
        else:
            if len(password) < 3 or len(password) > 20:
                raise forms.ValidationError('密码应在3-20位之间，请重新输入！')
        return password

    def clean_email(self):
        email= self.cleaned_data.get('email')
        wrongRegex = re.compile(r"\"?([-a-zA-Z0-9.?{}]+@\w+\.\w+)\"?")
        if re.match(wrongRegex,email):
            return email
        else:
            if re.match(wrongRegex, email) is not None:
                raise forms.ValidationError('邮箱不合法，请重新输入！')
            if len(email) < 3 or len(email) > 20:
                raise forms.ValidationError('邮箱应在3-20位之间，请重新输入！')
        return email
#修改用户表单
class UserupdateForm(forms.Form):

    username = forms.CharField(label='用户名',max_length=50)
    description = forms.CharField(label='描述',max_length=50)
    email = forms.EmailField(label='邮箱', max_length=50)

    def clean_name(self):
        username = self.cleaned_data.get('username')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(username) is not None:
            raise forms.ValidationError('用户名只能包含数字或者字母，请重新输入！')
        else:
            if len(username) < 3 or len(username) > 20:
                raise forms.ValidationError("用户名应在3-20位之间，请重新输入！")
        return username

    def clean_description(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description
    def clean_email(self):
        email= self.cleaned_data.get('email')
        wrongRegex = re.compile(r"\"?([-a-zA-Z0-9.?{}]+@\w+\.\w+)\"?")
        if re.match(wrongRegex,email):
            return email
        else:
            if re.match(wrongRegex, email) is not None:
                raise forms.ValidationError('邮箱不合法，请重新输入！')
            if len(email) < 3 or len(email) > 20:
                raise forms.ValidationError('邮箱应在3-20位之间，请重新输入！')
        return email
#修改组表单
class GroupupdateForm(forms.Form):

    groupname = forms.CharField(label='组名',max_length=50)
    description = forms.CharField(label='描述',max_length=50)

    def clean_name(self):
        groupname = self.cleaned_data.get('groupname')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(groupname) is not None:
            raise forms.ValidationError('组名只能包含数字或者字母，请重新输入！')
        else:
            if len(groupname) < 3 or len(groupname) > 20:
                raise forms.ValidationError("组名应在3-20位之间，请重新输入！")
        return groupname

    def description_name(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description
#创建组
class GroupcreateForm(forms.Form):
    groupname = forms.CharField(label='组名',max_length=50)
    description = forms.CharField(label='描述',max_length=50)
    def clean_name(self):
      groupname = self.cleaned_data.get('groupname')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(groupname) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(groupname) < 3 or len(groupname) > 20:
            raise forms.ValidationError("组名应在3-20位之间，请重新输入！")
      return groupname
    def clean_description(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description
#创建角色
class RolecreatForm(forms.Form):
    rolename = forms.CharField(label='组名',max_length=50)
    description = forms.CharField(label='描述', max_length=50)
    def clean_name(self):
      rolename = self.cleaned_data.get('rolename')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(rolename) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(rolename) < 3 or len(rolename) > 20:
            raise forms.ValidationError("角色应在3-20位之间，请重新输入！")
      return rolename
    def clean_description(self):
        description = self.cleaned_data.get('description')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search(description) is not None:
            raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
        else:
            if len(description) < 3 or len(description) > 20:
                raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
        return description
#编辑角色
class RoleUpdateForm(forms.Form):
        rolename = forms.CharField(label='组名', max_length=50)
        description = forms.CharField(label='描述', max_length=50)
        def clean_name(self):
            rolename = self.cleaned_data.get('rolename')
            wrongRegex = re.compile('[^a-zA-Z0-9]')
            if wrongRegex.search(rolename) is not None:
                raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
            else:
                if len(rolename) < 3 or len(rolename) > 20:
                    raise forms.ValidationError("角色应在3-20位之间，请重新输入！")
            return rolename

        def clean_description(self):
            description = self.cleaned_data.get('description')
            wrongRegex = re.compile('[^a-zA-Z0-9]')
            if wrongRegex.search(description) is not None:
                raise forms.ValidationError('描述只能包含数字或者字母，请重新输入！')
            else:
                if len(description) < 3 or len(description) > 20:
                    raise forms.ValidationError("描述应在3-20位之间，请重新输入！")
            return description
# 创建实例
class ServercreatForm(forms.Form):
    servername = forms.CharField(label='实例名称',max_length=50)
    def clean_name(self):
      servername = self.cleaned_data.get('servername')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(servername) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(servername) < 3 or len(servername) > 20:
            raise forms.ValidationError("实例应在3-20位之间，请重新输入！")
      return servername
# 创建快照
class ShotcreatForm(forms.Form):
    shotname = forms.CharField(label='快照名称',max_length=50)
    def clean_name(self):
      shotname = self.cleaned_data.get('shotname')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(shotname) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(shotname) < 3 or len(shotname) > 20:
            raise forms.ValidationError("快照名应在3-20位之间，请重新输入！")
      return shotname
# 创建镜像
class ImagecreatForm(forms.Form):
    imagename = forms.CharField(label='名称',max_length=50)
    container_format = forms.CharField(label='容器格式', max_length=50)
    disk_format = forms.CharField(label='磁盘格式', max_length=50)
    description = forms.CharField(label='描述', max_length=50)
    def clean_name(self):
      imagename = self.cleaned_data.get('imagename')
      wrongRegex = re.compile('[^a-zA-Z0-9]')
      if wrongRegex.search(imagename) is not None:
            raise forms.ValidationError('名称只能包含数字或者字母，请重新输入！')
      else:
        if len(imagename) < 3 or len(imagename) > 20:
            raise forms.ValidationError("实例应在3-20位之间，请重新输入！")
      return imagename

    def format_name(self):
        container_format = self.cleaned_data.get(' container_format')
        wrongRegex = re.compile('[^a-zA-Z0-9]')
        if wrongRegex.search( container_format) is not None:
            raise forms.ValidationError('磁盘格式只能包含数字或者字母，请重新输入！')
        else:
            if len( container_format) < 3 or len( container_format) > 20:
                raise forms.ValidationError("磁盘格式应在3-20位之间，请重新输入！")
        return  container_format
# 上传镜像
class FilecreatForm(forms.Form):
    path = forms.CharField(label='镜像路径',max_length=50)
    def clean_name(self):
        path = self.cleaned_data.get('path')
        if len(path) < 3 or len( path) > 20:
            raise forms.ValidationError("快照名应在3-20位之间，请重新输入！")
        return  path